package com.zhaofei.myapplication.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.zhaofei.myapplication.Bean.Level0Item;
import com.zhaofei.myapplication.Bean.Level1Item;
import com.zhaofei.myapplication.Bean.Level3;
import com.zhaofei.myapplication.MainActivity;
import com.zhaofei.myapplication.R;

import java.util.List;

/**
 * 版权：易金卡沃 版权所有
 * <p>
 * 作者：zhaofei
 * <p>
 * 创建日期：2018/6/8
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
public class Adapter_ExMore extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;
    public static final int TYPE_LEVEL_2 = 2;
    RecyclerView recyclerView;
    Context context;


    public Adapter_ExMore(List<MultiItemEntity> data, RecyclerView recyclerView, Context context) {
        super(data);
        this.recyclerView = recyclerView;
        addItemType(TYPE_LEVEL_0, R.layout.item2_m);
        addItemType(TYPE_LEVEL_1, R.layout.item3_m);
        addItemType(TYPE_LEVEL_2, R.layout.item4_m);
        this.context = context;
    }

    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {
        switch (item.getItemType()) {
            case TYPE_LEVEL_0:
                final Level0Item lv0 = (Level0Item) item;
                helper.setText(R.id.id_item_m, lv0.title)
                        .setImageResource(R.id.id_item_image, lv0.isExpanded() ? R.mipmap.up : R.mipmap.down);


                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {



                        int pos = helper.getAdapterPosition();
                        Log.d(TAG, "Level 0 item pos: " + pos);
                        if (lv0.isExpanded()) {
                            collapse(pos, true, true);

                            onItemAddClick.onItemClick(pos,false);

                        } else {
                            onItemAddClick.onItemClick(pos,true);

                            expand(pos, false, true);
//                            }
                        }
                    }
                });

                break;
            case TYPE_LEVEL_1:
                final Level1Item level1Item = (Level1Item) item;
                helper.setText(R.id.id_item_name, level1Item.title);

                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int pos = helper.getAdapterPosition();
                        if (level1Item.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });

                break;
            case TYPE_LEVEL_2:
                Level3 level3 = (Level3) item;
                helper.setText(R.id.id_item4_name, level3.getName());
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        MainActivity mainActivity=new MainActivity();
//                        mainActivity.showShare();
                    }
                });
                break;
        }
    }
    // add click callback
    OnAddClickListener onItemAddClick;
    public static interface OnAddClickListener {
        // true add; false cancel
        public void onItemClick(int position,boolean add); //传递boolean类型数据给activity
    }
    public void setOnAddClickListener(OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }
}
